#read in the Afro-Barometer data
setwd("~/Dropbox/stats2/6DataVisualization")
library(foreign)
r4<-read.dta("merged_r4_data.dta",convert.factors=F)

# subset each data.frame to just the country, radio question, gender, education, & urban/rural
r4<-subset(r4,select=c("Q12A", "country", "urbrur", "Q89", "Q101"))

#Q101 - 1=male, 2=female
#urbrur - 1=urban,2=rural
#country - 11=Mali

#code data as factors
table(r4$urbrur, exclude=NULL)
r4$rur<-factor(r4$urbrur, levels=c(1,2), labels=c("Urban","Rural"))

table(r4$Q101, exclude=NULL)
r4$gender<-factor(r4$Q101,levels=c(1,2), labels=c("Male","Female"))

r4$radio<-factor(r4$Q12A, levels=c(0,1,2,3,4), labels=c("Never","Less than once a month","A few times a month","A few times a week","Every day"))
table(r4$radio, exclude=NULL)

#subset to mali
r4<-r4[r4$country==11,]

#calculate percentages
urb.table<-table(r4$gender[r4$rur=="Urban"],r4$radio[r4$rur=="Urban"])
pct.urb<-prop.table(urb.table,1) #get percentages by row


rur.table<-table(r4$gender[r4$rur=="Rural"],r4$radio[r4$rur=="Rural"])
pct.rur<-prop.table(rur.table,1)#get percentages by row


pdf(file="bar.pdf", width=10,height=)
par(mfcol=c(2,1),las=2,mar=c(5,9,4,2))
barplot(pct.rur, beside=T,horiz=T, xaxt="n", cex.names=.8,xlim=c(0,.7),xlab="Proportion of Gender", main="Rural", legend.text=T)
axis(1,las=1)
barplot(pct.urb, beside=T,horiz=T, xaxt="n", cex.names=.8,xlim=c(0,.7),xlab="Proportion of Gender", main="Urban")
axis(1,las=1)
dev.off()

###############################
#not used below here
################################

library(ggplot2)
require(grid)

pdf(file="bar.pdf", width=10,height=10)
ggplot(r4, aes(gender,fill=radio))+geom_bar(position=position_dodge(width=1))+facet_wrap(~rur,)+scale_fill_grey("")+theme(legend.position="bottom",panel.grid.major.y=element_line(colour="grey"),panel.grid.minor=element_blank(),panel.background=element_rect(fill="white",colour="black"),panel.grid.major.y=element_line(colour="grey"),legend.key=element_rect(colour="black"),legend.background=element_rect(colour="black"),legend.key.height=unit(.5,"cm"),legend.key.width=unit(2,"cm"),text=element_text(family="Times"))+xlab("")+ylab("")
dev.off()
